<template>
  <img v-if="isVisible" :src="src" @load="onLoad" @error="onError" />
</template>

<script>
export default {
  props: {
    src: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      isVisible: false,
    };
  },
  mounted() {
    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          this.isVisible = true;
          observer.disconnect();
        }
      },
      {
        rootMargin: '50px',
      }
    );
    observer.observe(this.$el);
  },
  methods: {
    onLoad() {
      // handle successful image load
    },
    onError() {
      // handle broken image
    },
  },
};
</script>